###########################################
####     INSTRUMENTAL VARIABLES ANALYSES        
###########################################
data <- read.csv("data.csv") 
data$crisis <- 1*(data$growthc_t1 < -1)

cat("Table 5 of main text: enter values manually", '\n')
#  First stage regressions
ivfest <- NULL
ivfest[[1]] <- plm(crisis ~ growthcw_lag + lambda + I(s18f1_lag > 0) + gdp.gle_t1 + agedem_t1 + nelda19 + legelec + polityw_lag + poly(year, 3), data = data, index = c("country"), model="random")
ivfest[[2]] <- update(ivfest[[1]], model="within")
ivfest[[3]] <- update(ivfest[[1]], . ~ . - lambda - I(s18f1_lag > 0), model="random")
ivfest[[4]] <- update(ivfest[[3]], model="within")


ivest <- NULL
ivest[[1]] <- plm(mirt ~ crisis + lambda + I(s18f1_lag > 0)  + gdp.gle_t1 + agedem_t1 + nelda19 + legelec + polityw_lag + poly(year, 3)| growthcw_lag + lambda + I(s18f1_lag > 0)  + gdp.gle_t1 + agedem_t1 + nelda19 + legelec + polityw_lag + poly(year, 3), data=data, index=c("country"), model=c("random"))

ivest[[2]] <- update(ivest[[1]], model=c("within"))

ivest[[3]] <- plm(mirt ~ crisis + gdp.gle_t1 + agedem_t1 + nelda19 + legelec + polityw_lag + poly(year, 3)| growthcw_lag + gdp.gle_t1 + agedem_t1 + nelda19 + legelec + polityw_lag + poly(year, 3), data=data, index=c("country"), model=c("random"))
ivest[[4]] <- update(ivest[[3]], model=c("within"))

# First stage F stats
first <- function(t) {
  first <- pFtest(ivfest[[t]], update(ivfest[[t]], . ~ . - growthcw_lag))
  second <- c(coeftest(ivest[[t]], vcov = vcovHC(ivest[[t]], type = "HC0"))["crisis", c(1,2,4)])
  roundr(c(first$statistic, first$p.value, second), 2)
}

f <- sapply(c(1, 3), first)

cat("Table 2 of Appendix", '\n')
outtex(ivfest, below = FALSE, omit = c("year"), caption = "First-stage IV estimates.", var.labels = c("Intercept", "Proximity-weighted shocks", "Coup threats", "Protests", "GDP per capita", "Democracy age", "Foreign aid", "Legislative elections", "Weighted Policy IV"), file = "../tables/appendix_table2.tex", table.placement = 'h!', model.names = c("RE's", "FE's", "RE's", "FE's"), space = "20pt", size = "small")

cat("Table 3 of Appendix", '\n')
outtex(ivest, below = FALSE, omit = c("year"), robust = TRUE, caption = "Second-stage IV estimates.", var.labels = c("Intercept", "Crisis", "Coup threats", "Protests", "GDP per capita", "Democracy age", "Foreign aid", "Legislative elections", "Weighted Policy IV"), file = "../tables/appendix_table3.tex", table.placement = 'h!', model.names = c("RE's", "FE's", "RE's", "FE's"), space = "20pt", size = "small")

cat("Figure 8: Placebo test", '\n')
plotf <- function(q, title=NULL){
fit <- lm(cbind(mirt, growthcw_lag) ~ factor(country) + poly(year, 3) + lambda + I(s18f1_lag > 0) +  gdp.gle_t1 + agedem_t1 + nelda19 + legelec + polityw_lag, data = data, subset = open%in%c(q))
  e   <- data.frame(residuals(fit))
  print(summary(lm(e[,1] ~ e[,2])))
  ggplot(e, aes(mirt, growthcw_lag) ) + geom_point(alpha=1, color = "grey50") + geom_smooth(method="lm", se=TRUE, fullrange=FALSE, color = "black", size = 2) + ggtitle(title) + xlab("Proximity-weighted economic shock (residuals)") + ylab("Electoral manipulation (residuals)") + theme_bw() + theme(title = element_text(size=16 ), axis.title.x = element_text(size=12) , axis.title.y = element_text( size=12 )) + scale_y_continuous(breaks = seq(-.5,1.75, by = 0.25)) 
}

pdf("../graphs/placebo.pdf", width=10, height=5)
p1 <- plotf(0, title="Closed economies")
p2 <- plotf(1, title="Open economies")
grid.arrange(p1, p2, ncol = 2, main = "")
dev.off()
